package com.hskn.hss.utils;

import cn.hutool.core.date.DateUtil;
import com.hskn.hss.core.utils.DateUtils;
import com.hskn.hss.module.tlequip.entity.TlEquip;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AttributeUtils {
    /**
     * 对象属性为空判断(对象属性需要使用包装类型Integer Boolean)
     *
     * @param object
     * @return
     */
    public static boolean isNull(Object object) throws IllegalAccessException {
        Class clazz = (Class) object.getClass();
        Field fields[] = clazz.getDeclaredFields();
        boolean flag = true;
        Object fieldValue = null;
        for (Field field : fields) {
            fieldValue = field.get(object);
            if (fieldValue != null) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    /**
     * 对象属性赋值
     *
     * @param output
     * @return
     */
    public static <T, K> T byName(K input, T output, Class<K> c1, Class<T> c2) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
        Class inputClass = (Class) input.getClass();
        Field inputField[] = inputClass.getDeclaredFields();
        Object fieldValue = null;
        String fieldName = null;
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Object obj = (K) c1.newInstance();
        Method method = c1.getDeclaredMethod("add");
        method.setAccessible(true); // 加上这句即可解决问题！
        method.invoke((K) obj);

        Object obj1 = (T) c2.newInstance();
        Method method1 = c2.getDeclaredMethod("add");
        method1.setAccessible(true); // 加上这句即可解决问题！
        method.invoke((T) obj);
        for (Field field : inputField) {
            fieldValue = field.get(input);
            fieldName = field.getName();
            inputMap.put(fieldName, fieldValue);
        }
        for (String key : inputMap.keySet()) {
            BeanUtils.setProperty(output, key, inputMap.get(key));
        }
        return output;
    }

    /**
     * 请求时间补全
     *
     * @param time  请求时间
     * @param check 开始结束时间判断：s（开始时间）e（结束时间）
     * @return 为空不做处理，开始时间+00:00:00.0；结束时间+23:59:59.999
     */
    public static String timeCompletion(String time, char check) {
        String returnTime = new String();
        if (StringUtils.isNotEmpty(time)) {
            switch (check) {
                case 's':
                    returnTime = time + " 00:00:00.0";
                    break;
                case 'e':
                    returnTime = time + " 23:59:59.999";
                    break;
            }
            return returnTime;
        } else {
            return time;
        }
    }

    /**
     * 判断List中是否有重复的元素
     * @param ck
     * @param <K>
     * @return
     */
    public static <K> boolean RepeatedCheck(List<K> ck) {
        Set<K> cks = new HashSet<>(ck);
        if (ck.size() == cks.size()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 取得List中重复元素的坐标+1
     * @param ck
     * @param <K>
     * @param <E>
     * @return
     */
    public static <K,E> List<E> RepeatedList(List<K> ck) {
        List num = new ArrayList();
        for(int i=0;i<ck.size()-1;i++){
            for(int j=i+1;j<=ck.size()-1;j++){
                if(ck.get(i) == ck.get(j)){
                    num.add(i+1);
                    num.add(j+1);
                }
            }
            if(CollectionUtils.isNotEmpty(num)){
                num = (ArrayList) num.stream().distinct().collect(Collectors.toList());
            }
        }
        return num;
    }

    public static boolean IntervalComparison(String a , String b) throws ParseException {

        boolean check = false;
        String[] aList = a.split("-");
        String[] bList = b.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

        Date astr = sdf.parse(aList[0]);//a区间开始
        Date bstr = sdf.parse(bList[0]);//b区间开始
        Date aend = sdf.parse(aList[1]);//a区间结束
        Date bend = sdf.parse(bList[1]);//b区间结束

        //第二段结束时间比第一段开始时间早
        if (bend.before(astr)) {
            check = true;
        }
        //第二段开时间比第一段结束时间晚
        if (aend.before(bstr)) {
            check = true;
        }
        return check;
    }

    /**
     * 没有重叠返回true
     * @param list
     * @return
     * @throws ParseException
     */
    public static boolean IntervalComparisonAll(List<String> list) throws ParseException {
        for (int i=1;i<=list.size();i++){
            for(int j = i+1;j<=list.size();j++){
                if(!AttributeUtils.IntervalComparison(list.get(i-1), list.get(j-1))){
                    return false;
                }
            }
        }
        return true;
    }
}

